home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex14-2.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  90 lines

  1. // ex14-2.c -- Counted pointers
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex14-2.c,v 3.0 90/05/15 22:44:57 kgorlen Rel $
  4.  
  5. #include "Patient.h"
  6. #include "IdentDict.h"
  7. #include "AssocInt.h"
  8.  
  9. class RefCountTable: public NIHCL {
  10.     static IdentDict t;
  11. public:
  12.     static unsigned inc(const Object*);
  13.     static unsigned dec(const Object*);
  14.     static void printOn(ostream& strm =cout) {
  15.         t.printOn(strm); strm << endl;
  16.     }
  17. };
  18.  
  19. unsigned RefCountTable::inc(const Object* p)
  20. {
  21. // Find ref count
  22.     AssocInt* a =
  23.         AssocInt::castdown((Object*)t.assocAt(*p));
  24.     if (!a) {                               // no ref count for p
  25.         t.add(*new AssocInt(*(Object*)p,1));    // create entry,
  26.         return 1;                               // ref count=1
  27.     }
  28.     Integer& refct = *Integer::castdown(a->value());
  29.     return refct.value(refct.value()+1);    // increment ref count
  30. }
  31.  
  32. unsigned RefCountTable::dec(const Object* p)
  33. {
  34.     Integer& refct = *Integer::castdown(t.atKey(*p));
  35.     unsigned n = refct.value(refct.value()-1);
  36.     if (n == 0) delete t.removeKey(*p);
  37.     return n;
  38. }
  39.  
  40. IdentDict RefCountTable::t;
  41.  
  42. class Patient_CP: public NIHCL {
  43.     Patient* p;
  44. public:
  45.     Patient_CP(Patient* pt)     { RefCountTable::inc(p = pt); }
  46.     Patient_CP(const Patient_CP& cp)
  47.         { RefCountTable::inc(p = cp.p); }
  48.     ~Patient_CP()
  49.         { if (RefCountTable::dec(p)==0) delete p; }
  50.     Patient_CP& operator=(const Patient_CP&);
  51.     Patient& operator*()        { return *p; }
  52.     Patient& operator[](int i)  { return p[i]; }
  53.     Patient* operator->()       { return p; }
  54. };
  55.  
  56. Patient_CP& Patient_CP::operator=(const Patient_CP& cp)
  57. {
  58.     if (p != cp.p) {
  59.         if (RefCountTable::dec(p) == 0) delete p;
  60.         RefCountTable::inc(p = cp.p);
  61.     }
  62.     return *this;
  63. }
  64.  
  65. main()
  66. {
  67.     Patient_CP cp1 =
  68.         new Patient("Doe, John E","123-45-6789",20892);
  69.     Patient_CP cp2 =
  70.         new Patient("Doe, Jane F","987-65-4321",20892);
  71.     Patient_CP* cpp = new Patient_CP(cp1);
  72.  
  73.     cout << "Initial RefCountTable:" << endl;
  74.     RefCountTable::printOn(); cout << endl;
  75.  
  76.     cp1->printOn(); cout << endl;
  77.     (*cp2).printOn(); cout << endl;
  78.     (*cpp)->printOn(); cout << endl;
  79.  
  80.     cout << endl;
  81.     cp2 = cp1;
  82.     cp2->printOn(); cout << endl;
  83.     cout << "\nRefCountTable after cp2 = cp1:" << endl;
  84.     RefCountTable::printOn();
  85.  
  86.     delete cpp;
  87.     cout << "\nRefCountTable after delete cpp:" << endl;
  88.     RefCountTable::printOn();
  89. }
  90.